/*
    Space Simulation - Simulate the solar system
    Copyright (C) 2010  Guillaume Andrieu and Sylvain Martin

    This file is part of Space Simulation.

    Space Simulation is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Space Simulation is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Space Simulation.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <string.h>

#include "Particule.h"
#include "Vector3D.h"
#include "Simulation.h"
#include "physic.h"
#include "display/display.h"

/*
 * Valeur reelle aleatoire, entre 0 et 1
 */
double frand()
{
  return ((float) rand()) / ((float) RAND_MAX);
}

float last_explosion = 0.0;
int nbr_particules;

/*
 * Fonction de mise a jour
 * Genere periodiquement une explosion
 */
void update_explosion()
{
  if(simul.space.time - last_explosion > 5.0)
  {
  last_explosion = simul.space.time;
    int i;
    float mod, arg;
    for(i = 0; i < nbr_particules; i++)
    {
      mod = 1*frand();
      arg = 2*M_PI*frand();
      simul.particules[1+i] = MakeParticule("particule", MakeVector3D(mod*cos(arg), mod*sin(arg), 0.0), MakeVector3D(5*mod*cos(arg), 5*mod*sin(arg)+10, 0.0), 0.1, 0.01);
    }
  }
}

/*
 * Fonction principale
 */
int
main (int argc, char ** argv)
{
  if(argc < 2)
  {
    printf("Erreur ! \nUtilisation : %s nbr_particules\nVoir README.explosion pour plus de details\n", argv[0]);
    return -1;
  }
  nbr_particules = atoi(argv[1]);
  
  Space space;
  space.G = 6.67428E-11;
  space.time = 0.0;
  
  simul = MakeSimulation(space, time(NULL), 30E-5, 0.05);
  
  /* On ajoute la terre, pour creer une gravite terrestre */
  Particule earth = MakeParticule("Earth", MakeVector3D(0.0, -6371.01E3, 0.0), MakeVector3D(0.0, 0.0, 0.0), 5.9736E24, 6371.01E3);
  addParticule(&simul, earth);
  
  /*
   * Definit la graine du generateur pseudo aleatoire
   */
  srand(time(NULL));
  
  /*
   * Cree les particules, et l'explosion initiale a t = 0
   */
  int i;
  Particule object;
  float mod, arg, arg2;
  for(i = 0; i < nbr_particules; i++)
  {
    mod = 1.0*frand();
    arg = 2.0*M_PI*frand();
    arg2 = 2.0*M_PI*frand();
    object = MakeParticule("particule", MakeVector3D(mod*cos(arg), mod*sin(arg), mod*sin(arg2)), MakeVector3D(5.0*mod*cos(arg), 5.0*mod*sin(arg)+10.0, 5.0*mod*sin(arg2)), 0.1, 0.01);
    addParticule(&simul, object);
  }

  display_init(&argc, argv);
  /*  creerFenetre (600, 600, "Explosion"); */
  
  /* Echelle (m/pixel) */
  display_scale = 100.0/50.0;
  
  /* Fonction de mise a jour */
  /* funcMaj = update_explosion; */
  
  display_loop();
  
  return 0;
}
